//
//  WYTDeskVC.m
//  Diary
//
//  Created by lanou3g on 15/10/29.
//  Copyright © 2015年 王艺拓. All rights reserved.
//

#import "WYTDeskVC.h"
#import "WYTDestView.h"
#import "WYTDiaryInfoSelectionVC.h"
#import "WYTDiaryInfoSelectionView.h"
#import "LYLTimeLineVC.h"
#import "GHAllDiaryVC.h"
#import "PasswordVC.h"

#import "WYTEditingViewA.h"
#import "LYLEditingViewB.h"
#import "GHEditingViewC.h"
#import "GHEditingViewD.h"
#import "LYLEditingViewE.h"
#import "LYLEditingViewF.h"

#import "AppDelegate.h"
#import "GHDiaryDataHandle.h"
#import "UIImage+CK_ShutScreen.h"
#import "WYTTimeTool.h"
#import "GHDiaryDataModel.h"

#import "LYLResetPasswordView.h"
#import "WYTSyncView.h"
#import "WYTCacheView.h"
#import "WYTContactUSView.h"
#import "WYTAboutUSView.h"

#import "FZLDiaryDataObject.h"

static int UpCount = 0; // 上传数据计数

// ------- 日记本 ------

//开始日记时右上角的button
#define kEndEditingButtonSide 30 //边长
#define kEndEditingButtonRightInset kScreenWidth - kScreenWidth * 0.05 - kEndEditingButtonSide //button尾巴距离右边屏边缘

//日记编辑界面
#define kEditingBoardViewWidth kScreenWidth * 0.95 //宽
#define kEditingBoardViewHeight kScreenHeight * 0.838 //高

//日记编辑模板ScrollView
#define kPageModelScrollViewHeight kScreenHeight * 0.07 //高
#define kPageModelScrollViewWidth kScreenHeight * 0.07 //宽
#define kPageModelScrollViewMinY kScreenHeight * 0.9 //MinY
#define kPageModelButtonSide kPageModelScrollViewHeight * 1.5 //边长
#define kPageSubViewsRemoveY kScreenHeight*0.123 // 弹出键盘上移距离

// ------- 九宫格 ------
#define kFunctionScrollViewMinY kScreenHeight * 0.208
#define kFunctionScrollViewHeight kScreenHeight * 0.65
// ------- 清除缓存 ------
#define kCachePath  [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]
#define kDocumentPath   [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]

#import "FZLShareView.h"
#import "UMSocial.h"

@interface WYTDeskVC ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate,UMSocialUIDelegate,UITextFieldDelegate>

@property (nonatomic, strong)WYTDestView *rootView;

// 日记编辑界面(FunctionViewB)
@property (nonatomic, strong)UIVisualEffectView *diaryInfoSelectionBlurView;
@property (nonatomic, strong)WYTDiaryInfoSelectionView *infoSelectionView;
@property (nonatomic, strong)UIImageView *diaryCaptureImageView;//缩小前截图所在的imageview
@property (nonatomic, strong)UIImageView *screenShotImageViewOnFunctionImageViewB;//缩小后截图所在的
@property (nonatomic, strong)WYTEditingViewA *editingViewA;
@property (nonatomic, strong)LYLEditingViewB *editingViewB;
@property (nonatomic, retain) GHEditingViewC *editingViewC;
@property (nonatomic, retain) GHEditingViewD *editingViewD;
@property (nonatomic, strong)LYLEditingViewE *editingViewE;
@property (nonatomic, strong)LYLEditingViewF *editingViewF;
@property (nonatomic, assign) NSInteger chickedTag;
@property (nonatomic, assign) CGPoint diaryEditingBoardViewOriginalCenter;
@property (nonatomic, assign) CGPoint diaryStartEditingImageViewOriginalCenter;

// 临时数据库文件
@property (nonatomic, retain) NSManagedObjectContext *manageContext;
// 存储数据库数据的数组
@property (nonatomic, retain) GHDiaryDataHandle *handle;
// 分享的View
@property (nonatomic, retain) FZLShareView *shareView;
@property (nonatomic, retain) UIImage *shareImage;

// GHDiaryDataModel
@property (nonatomic, retain) GHDiaryDataModel *dataModel;
@property (nonatomic, strong) LYLResetPasswordView *resetPwView;
@property (nonatomic, strong)WYTSyncView *syncView;
@property (nonatomic, strong)WYTCacheView *cacheView;
@property (nonatomic, strong)WYTContactUSView *contactUSView;
@property (nonatomic, strong)WYTAboutUSView *aboutUSView;
// 线程队列
@property (nonatomic, retain) NSOperationQueue *queue;

@property (nonatomic, retain) UILabel *tipsView;

@end

@implementation WYTDeskVC

#pragma mark - 主页面:FunctionScrollView
- (void)viewDidLoad {
    [super viewDidLoad];
    self.handle = [[GHDiaryDataHandle alloc] init];
  
    [self setUpView];
    
    [self setUpNotificationCenter];
    [self setUpDelegate];
    [self setUpButtonActions];
    [self registerForKeyboardNotifications];
    [self addChickedActionForSettingBtn];
    //[self setUpGestures];
    //登录注册界面点击事件
    [self setUpLoginRegisterActions];
    self.rootView.functionImageViewA.userInteractionEnabled = YES;

    //NSString *path = NSHomeDirectory();//主目录
    //NSLog(@"NSHomeDirectory:%@",path);
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    if (self.diaryInfoModel != nil) {
        self.editingViewA.dateLabel.text = self.diaryInfoModel.timeString;
        self.editingViewA.weatherIconImageView.image = [UIImage imageNamed:self.diaryInfoModel.weatherIconName];
        self.editingViewB.dateLabel.text = self.diaryInfoModel.timeString;
        self.editingViewB.placeLabel.text = self.diaryInfoModel.locationString;
        self.editingViewB.weatherImg.image = [UIImage imageNamed:self.diaryInfoModel.weatherIconName];
        self.editingViewC.authorLabel.text = self.diaryInfoModel.timeString;
        self.editingViewC.addressLabel.text = self.diaryInfoModel.locationString;
        self.editingViewC.weatherPicImg.image = [UIImage imageNamed:self.diaryInfoModel.weatherIconName];
        self.editingViewD.authorAndDateLabel.text = self.diaryInfoModel.timeString;
        self.editingViewD.addressLabel.text = self.diaryInfoModel.locationString;
        self.editingViewD.weatherImage.image = [UIImage imageNamed:self.diaryInfoModel.weatherIconName];
        self.editingViewE.dateLabel.text = self.diaryInfoModel.timeString;
        self.editingViewE.placeLabel.text = self.diaryInfoModel.locationString;
        self.editingViewE.weatherImg.image = [UIImage imageNamed:self.diaryInfoModel.weatherIconName];
        self.editingViewF.dateLabel.text = self.diaryInfoModel.timeString;
        self.editingViewF.placeLabel.text = self.diaryInfoModel.locationString;
        self.editingViewF.weatherImg.image = [UIImage imageNamed:self.diaryInfoModel.weatherIconName];
    }else{
        
        self.editingViewA.dateLabel.text = [WYTTimeTool currentDate_yearMonthDateHourMinutesSeparatByDiagonals];
        self.editingViewA.weatherIconImageView.image = [UIImage imageNamed:@"sunny_gray"];
        self.editingViewB.dateLabel.text = [WYTTimeTool currentDate_yearMonthDateHourMinutesSeparatByDiagonals];
        self.editingViewB.weatherImg.image = [UIImage imageNamed:@"sunny_gray"];
        self.editingViewC.authorLabel.text = [WYTTimeTool currentDate_yearMonthDateHourMinutesSeparatByDiagonals];
        self.editingViewC.weatherPicImg.image = [UIImage imageNamed:@"sunny_gray"];
        self.editingViewD.authorAndDateLabel.text = [WYTTimeTool currentDate_yearMonthDateHourMinutesSeparatByDiagonals];
        self.editingViewD.weatherImage.image = [UIImage imageNamed:@"sunny_gray"];
        self.editingViewE.dateLabel.text = [WYTTimeTool currentDate_yearMonthDateHourMinutesSeparatByDiagonals];
        self.editingViewE.weatherImg.image = [UIImage imageNamed:@"sunny_gray"];
        self.editingViewF.dateLabel.text = [WYTTimeTool currentDate_yearMonthDateHourMinutesSeparatByDiagonals];
        self.editingViewF.weatherImg.image = [UIImage imageNamed:@"sunny_gray"];
    }
}

- (void)modelTransmiting:(WYTDiaryInfoModel *)model{
    self.diaryInfoModel = [[WYTDiaryInfoModel alloc]init];
    self.diaryInfoModel = model;
}

- (void)setUpView{
    self.rootView = [[WYTDestView alloc]init];
    self.rootView.frame = [UIScreen mainScreen].bounds;
    self.view = self.rootView;
    // 次页 日记编辑界面(FunctionViewB) 添加视图模板
    [self setUpEditingViews];
    // 分享界面
    [self addShareView];
}
#pragma SettingViewBtn点击事件
- (void)addChickedActionForSettingBtn
{
    self.rootView.functionImageViewC.userInteractionEnabled = YES;
    [self.rootView.setttingsView.lockButton addTarget:self action:@selector(chickSettingBtn:) forControlEvents:1<<6];
    [self.rootView.setttingsView.syncButton addTarget:self action:@selector(chickSettingBtn:) forControlEvents:1<<6];
    [self.rootView.setttingsView.cacheButton addTarget:self action:@selector(chickSettingBtn:) forControlEvents:1<<6];
    [self.rootView.setttingsView.contactUSButton addTarget:self action:@selector(chickSettingBtn:) forControlEvents:1<<6];
    [self.rootView.setttingsView.aboutUSButton addTarget:self action:@selector(chickSettingBtn:) forControlEvents:1<<6];
}

- (void)chickSettingBtn:(UIButton *)sender
{
    switch (sender.tag) {
        case 386010:
            [self setUpResetPwView];
            break;
        case 386011:
            [self setUpSyncView];
            break;
        case 386012:
            [self setUpCacheView];
            break;
        case 386013:
            [self setUpaboutUSView];
            break;
        case 386014:
            [self setUpContactUSView];
            NSLog(@"chickSettingBtn- - %ld", sender.tag);
            break;
        default:
            break;
    }
}

- (void)setUpResetPwView
{
    self.resetPwView = [[LYLResetPasswordView alloc] init];
    self.resetPwView.frame = CGRectMake(0, 0, CGRectGetWidth(self.rootView.setttingsView.frame), CGRectGetHeight(self.rootView.setttingsView.frame));
    self.resetPwView.alpha = 0;
    [self.rootView.setttingsView addSubview:self.resetPwView];
    
    [UIView animateWithDuration:0.3 animations:^{
        self.resetPwView.alpha = 1;
    }];
    
    UISwipeGestureRecognizer *swipeToDesk = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(syncViewSwipeToDesk)];
    swipeToDesk.direction = UISwipeGestureRecognizerDirectionDown;
    [self.resetPwView addGestureRecognizer:swipeToDesk];
    
    [self.resetPwView.confirmButton addTarget:self action:@selector(chickForResetPw) forControlEvents:UIControlEventTouchUpInside];
}

- (void)setUpSyncView{
    self.syncView = [[WYTSyncView alloc]init];
    //NSLog(@"self.rootView.setttingsView.frame = %f", CGRectGetWidth(self.rootView.setttingsView.frame));
    //NSLog(@"self.rootView.setttingsView.frame = %f", CGRectGetHeight(self.rootView.setttingsView.frame));
    self.syncView.frame = CGRectMake(0, 0, CGRectGetWidth(self.rootView.setttingsView.frame), CGRectGetHeight(self.rootView.setttingsView.frame));
    self.syncView.alpha = 0;
    [self.rootView.setttingsView addSubview:self.syncView];
    
    [UIView animateWithDuration:0.3 animations:^{
        self.syncView.alpha = 1;
    }];
    
    UISwipeGestureRecognizer *swipeToDesk = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(syncViewSwipeToDesk)];
    swipeToDesk.direction = UISwipeGestureRecognizerDirectionDown;
    [self.syncView addGestureRecognizer:swipeToDesk];
    
    [self.syncView.confirmButton addTarget:self action:@selector(syncViewConfirmButtonDidClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.syncView.syncDownButton addTarget:self action:@selector(syncViewsyncDownButtonDidClicked:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)syncViewSwipeToDesk{
    [UIView animateWithDuration:0.3 animations:^{
            self.syncView.alpha = 0;
        self.resetPwView.alpha = 0;
    }];
    [self performSelector:@selector(delayToRemoveSyncView) withObject:self afterDelay:0.4];
}

- (void)delayToRemoveSyncView{
    if (self.syncView != nil) {
        [self.syncView removeFromSuperview];
        self.syncView = nil;
    } else if (self.resetPwView != nil){
        [self.resetPwView removeFromSuperview];
        self.resetPwView = nil;
    }
}

- (void)chickForResetPw
{
    PasswordVC *passWordVC = [[PasswordVC alloc] init];
    passWordVC.setNewPassWord = YES;
    [self presentViewController:passWordVC animated:NO completion:nil];
}

#pragma -----点击了同步按钮
- (void)syncViewConfirmButtonDidClicked:(UIButton *)sender{
    AVUser *user = [AVUser currentUser];
    if (user) {
    GHDiaryDataHandle *diaryData = [[GHDiaryDataHandle alloc] init];
    NSMutableArray *diaryDataArray = [diaryData getModelArrayWithManagedObjectContext:self.manageContext];
    if (diaryDataArray.count == 0) {
        WYTAlertView *alertView = [WYTAlertView setUpAlertViewWithSucceed:NO alertLabelText:@"没有数据可以同步"];
        [self.rootView addSubview:alertView];
    } else {
        // 添加提示文字
        self.tipsView.frame = CGRectMake(kScreenWidth * 2,  20, kScreenWidth, kScreenWidth * 0.805 * 0.4 / 8);
        self.tipsView.textAlignment = NSTextAlignmentCenter;
        self.tipsView.textColor = [UIColor whiteColor];
//        [self.rootView.functionScrollView addSubview:self.tipsView];
        self.tipsView.text = @"正在同步至云端..."; // 同步完成后再次出现更改文字
        [self.rootView.functionScrollView addSubview:self.tipsView];
        for (GHDiaryDataModel *dataModel in diaryDataArray) {
            AVQuery *query = [FZLDiaryDataObject query];
            [query whereKey:@"diaryID" equalTo:dataModel.diaryID];
            [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
                UpCount ++; // 每次循环该计数加1
                if (objects.count == 0) {
                    [self savaDataWithDiaryModel:dataModel];
                    NSString *AVPaaFilesPath = [kCachePath stringByAppendingString:@"/AVPaasFiles"];
                    NSFileManager *manager = [NSFileManager defaultManager];
                    [manager removeItemAtPath:AVPaaFilesPath error:nil];
                } else {
                    //NSLog(@"已经有了");
                    [self performSelector:@selector(successedSyn) withObject:self afterDelay:0.3];
                }
//                NSLog(@"UpCount=======%d", UpCount);
                if (UpCount == diaryDataArray.count) {
                    self.tipsView.text = @"同步至云端完成"; // 同步完成
                    UpCount = 0; // 重置计数
                    [self performSelector:@selector(successedSyn) withObject:self afterDelay:0.3];
//                    NSLog(@"同步至云端完成");
                }
            }];
        }
        self.tipsView.text = @"同步至云端完成"; // 同步完成
        [self performSelector:@selector(successedSyn) withObject:self afterDelay:0.3];
    }
    for (GHDiaryDataModel *dataModel in diaryDataArray) {
        AVQuery *query = [FZLDiaryDataObject query];
        [query whereKey:@"diaryID" equalTo:dataModel.diaryID];
       [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
           if (objects.count == 0) {
               [self savaDataWithDiaryModel:dataModel];
               NSString *AVPaaFilesPath = [kCachePath stringByAppendingString:@"/AVPaasFiles"];
               NSFileManager *manager = [NSFileManager defaultManager];
               [manager removeItemAtPath:AVPaaFilesPath error:nil];
           } else {
               //NSLog(@"已经有了");
               [self performSelector:@selector(successedSyn) withObject:self afterDelay:0.3];
           }
        }];
    }
    } else {
        WYTAlertView *alerView = [WYTAlertView setUpAlertViewWithSucceed:NO alertLabelText:@"登陆之后才能同步"];
        [self.view addSubview:alerView];
    }
}

#pragma mark - 同步到云端
- (void)successedSyn
{
    self.tipsView.frame = CGRectMake(kScreenWidth * 0.805 * 0.11,  kScreenWidth * 0.5, 0, 0);
    [self hiddenViewAftersyncDown];
}

- (void)hiddenViewAftersyncDown{
    [UIView animateWithDuration:0.3 animations:^{
        self.syncView.alpha = 0;
    }];
    [self performSelector:@selector(delayToRemoveSyncView) withObject:self afterDelay:0.4];
}

#pragma mark 云端数据下载到本地
- (void)syncViewsyncDownButtonDidClicked:(UIButton *)sender{
    AVUser *user = [AVUser currentUser];
    if (user) {
        self.tipsView.frame = CGRectMake(kScreenWidth * 2,  20, kScreenWidth, kScreenWidth * 0.805 * 0.4 / 8);
        self.tipsView.textAlignment = NSTextAlignmentCenter;
        self.tipsView.textColor = [UIColor whiteColor];
        [self.rootView.functionScrollView addSubview:self.tipsView];
        self.tipsView.text = @"数据获取中,请稍后...";
        AVRelation *relation = [user relationforKey:@"diary"];
        [[relation query] findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
            if (!error) {
        // 云端下载数据数组在这里面
                NSLog(@"objects = %@",objects);
                if (objects.count > 0) {
                    NSBlockOperation *operat = [NSBlockOperation blockOperationWithBlock:^{
//                        self.tipsView.text = @"数据获取中,请稍后...";
                        for (FZLDiaryDataObject *obj in objects) {
                            [self.handle saveDataWithManageContext:self.manageContext AndWithModel:[self.handle getModelWithFZLModel:obj]];
                        }
//                        self.tipsView.text = @"从云端同步至本设备结束";
//                        [self performSelector:@selector(successedSyn) withObject:self afterDelay:0.3]; // 不走
                    }];
                    [self.queue addOperation:operat];
                } else {
                    // 云端没有数据
                   WYTAlertView *alert = [WYTAlertView setUpAlertViewWithSucceed:NO alertLabelText:@"您尚未使用过云端"];
                    [self.view addSubview:alert];
//                    self.tipsView.text = @"您尚未上传至云端";
                }
            } else {
                // 下载错误
                NSLog(@"%@",error);
               WYTAlertView *alert = [WYTAlertView setUpAlertViewWithSucceed:NO alertLabelText:@"从云端同步至本设备发生未知错误"];
                [self.view addSubview:alert];
//                self.tipsView.text = @"从云端同步至本设备发生未知错误";
         }
        }];
//        self.tipsView.text = @"从云端同步至本设备结束";
        [self performSelector:@selector(successedSyn) withObject:self afterDelay:3];
    } else {
        WYTAlertView *alerView = [WYTAlertView setUpAlertViewWithSucceed:NO alertLabelText:@"登陆才能同步"];
        [self.view addSubview:alerView];
    }
}

- (void)savaDataWithDiaryModel:(GHDiaryDataModel *) dataModel
{
    FZLDiaryDataObject *object = [[FZLDiaryDataObject alloc] init];
        object.diaryID = dataModel.diaryID;
        object.clickTag = dataModel.clickTag;
        if (![dataModel.photoImg isEqual:@""]) {
            NSData *photoImageData = UIImagePNGRepresentation(dataModel.photoImg);
                object.photoImg = [AVFile fileWithData:photoImageData];
          }
        object.title = dataModel.title;
        object.subtitle = dataModel.subtitle;
        object.summary = dataModel.summary;
        object.dateLabelText = dataModel.dateLabelText;
        object.placeLabel = dataModel.placeLabel;
        NSData *diaryImageData = UIImagePNGRepresentation(dataModel.diaryImage);
        object.diaryImage = [AVFile fileWithData:diaryImageData];
        [object saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            if (succeeded) {
                AVUser *user = [AVUser currentUser];
                AVRelation *relation = [user  relationforKey:@"diary"];
                [relation addObject:object];
                [user saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                    if (succeeded) {
                    //NSLog(@"收藏成功");
                    }
                }];
//                WYTAlertView *alert = [WYTAlertView setUpAlertViewWithSucceed:YES alertLabelText:@"正在上传"];
//                [self.view addSubview:alert];
            } else {
                WYTAlertView *alerView = [WYTAlertView setUpAlertViewWithSucceed:NO alertLabelText:@"同步失败"];
                [self.view addSubview:alerView];
            }
        }];
}

- (void)setUpCacheView{
    self.cacheView = [[WYTCacheView alloc]init];
    self.cacheView.frame = CGRectMake(0, 0, CGRectGetWidth(self.rootView.setttingsView.frame), CGRectGetHeight(self.rootView.setttingsView.frame));
    self.cacheView.alpha = 0;
    [self.rootView.setttingsView addSubview:self.cacheView];
    
    [UIView animateWithDuration:0.3 animations:^{
        self.cacheView.alpha = 1;
    }];
    
    UISwipeGestureRecognizer *swipeToDesk = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(cacheViewSwipeToDesk)];
    swipeToDesk.direction = UISwipeGestureRecognizerDirectionDown;
    [self.cacheView addGestureRecognizer:swipeToDesk];

//    float cacheSize = [self folderSizeAtPath:kCachePath];
//    float documentSize = [self folderSizeAtPath:kDocumentPath];
//    [self.cacheView.confirmButton setTitle:[NSString stringWithFormat:@"确认清除(%.2fM)",cacheSize + documentSize] forState:UIControlStateNormal];

    [self.cacheView.confirmButton addTarget:self action:@selector(cacheViewConfirmButtonDidClicked:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)cacheViewSwipeToDesk{
    [UIView animateWithDuration:0.3 animations:^{
        self.cacheView.alpha = 0;
    }];
    [self performSelector:@selector(delayToRemoveCacheView) withObject:self afterDelay:0.4];
}

- (void)delayToRemoveCacheView{
    [self.cacheView removeFromSuperview];
    self.cacheView = nil;
}
#pragma mark 点击了"清除缓存按钮"
- (void)cacheViewConfirmButtonDidClicked:(UIButton *)sender{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"删除成功" delegate:nil cancelButtonTitle:nil otherButtonTitles: nil];
    [alert show];
    [self performSelector:@selector(alertViewCancel:) withObject:alert afterDelay:0.3];
    [self clearCache:kCachePath];
    [self.handle deletAllDataWithManageContext:self.manageContext];
}

- (void)alertViewCancel:(UIAlertView *)alertView
{
    [alertView dismissWithClickedButtonIndex:0 animated:YES];
    [self delayToRemoveCacheView];
}

- (void)setUpContactUSView{
    self.contactUSView = [[WYTContactUSView alloc]init];
    self.contactUSView.frame = CGRectMake(0, 0, CGRectGetWidth(self.rootView.setttingsView.frame), CGRectGetHeight(self.rootView.setttingsView.frame));
    self.contactUSView.alpha = 0;
    [self.rootView.setttingsView addSubview:self.contactUSView];
    
    [UIView animateWithDuration:0.3 animations:^{
        self.contactUSView.alpha = 1;
    }];
    
    UISwipeGestureRecognizer *swipeToDesk = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(contactUSViewSwipeToDesk)];
    swipeToDesk.direction = UISwipeGestureRecognizerDirectionDown;
    [self.contactUSView addGestureRecognizer:swipeToDesk];
    
    [self.contactUSView.confirmButton addTarget:self action:@selector(contactUSViewConfirmButtonDidClicked:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)contactUSViewSwipeToDesk{
    [UIView animateWithDuration:0.3 animations:^{
        self.contactUSView.alpha = 0;
    }];
    [self performSelector:@selector(delayToRemovecontactUSView) withObject:self afterDelay:0.4];
}

- (void)contactUSViewConfirmButtonDidClicked:(UIButton *)sender{
    if (self.contactUSView.descriptionLabel.text.length > 0) {
        AVObject *post = [AVObject objectWithClassName:@"Feedback"];
        AVUser *user = [AVUser currentUser];
        [post setObject:self.contactUSView.descriptionLabel.text forKey:@"suggestion"];
        [post setObject:user.objectId forKey:@"userID"];
        [post setObject:user.username forKey:@"userName"];
        [post saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            if (succeeded) {
                [UIView animateWithDuration:0.3 animations:^{
                    self.contactUSView.alpha = 0;
                }];
                [self performSelector:@selector(delayToRemovecontactUSView) withObject:self afterDelay:0.4];
                WYTAlertView *alerView = [WYTAlertView setUpAlertViewWithSucceed:YES alertLabelText:@"提交成功,谢谢您的支持!"];
                [self.view addSubview:alerView];
            }
        }];
    } else {
        [UIView animateWithDuration:0.3 animations:^{
            self.contactUSView.alpha = 0;
        }];
        [self performSelector:@selector(delayToRemovecontactUSView) withObject:self afterDelay:0.4];
    }
}

- (void)delayToRemovecontactUSView{
    [self.contactUSView removeFromSuperview];
    self.contactUSView = nil;
}

- (void)setUpaboutUSView{
    self.aboutUSView = [[WYTAboutUSView alloc]init];
    self.aboutUSView.frame = CGRectMake(0, 0, CGRectGetWidth(self.rootView.setttingsView.frame), CGRectGetHeight(self.rootView.setttingsView.frame));
    self.aboutUSView.alpha = 0;
    [self.rootView.setttingsView addSubview:self.aboutUSView];
    
    [UIView animateWithDuration:0.3 animations:^{
        self.aboutUSView.alpha = 1;
    }];
    
    UISwipeGestureRecognizer *swipeToDesk = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(aboutUSViewSwipeToDesk)];
    swipeToDesk.direction = UISwipeGestureRecognizerDirectionDown;
    [self.aboutUSView addGestureRecognizer:swipeToDesk];
    
    [self.aboutUSView.confirmButton addTarget:self action:@selector(aboutUSViewConfirmButtonDidClicked) forControlEvents:UIControlEventTouchUpInside];
}

- (void)aboutUSViewConfirmButtonDidClicked
{
    [UIView animateWithDuration:0.3 animations:^{
        self.aboutUSView.alpha = 0;
    }];
    [self performSelector:@selector(delayToRemoveaboutUSView) withObject:self afterDelay:0.4];
}

- (void)aboutUSViewSwipeToDesk{
    [UIView animateWithDuration:0.3 animations:^{
        self.aboutUSView.alpha = 0;
    }];
    [self performSelector:@selector(delayToRemoveaboutUSView) withObject:self afterDelay:0.4];
}

- (void)delayToRemoveaboutUSView{
    [self.aboutUSView removeFromSuperview];
    self.aboutUSView = nil;
}

// 计算目录大小
- (float)folderSizeAtPath:(NSString *)path{
    NSFileManager *fileManager=[NSFileManager defaultManager];
    float folderSize;
    if ([fileManager fileExistsAtPath:path]) {
        NSArray *childerFiles = [fileManager subpathsAtPath:path];
        for (NSString *fileName in childerFiles) {
            NSString *absolutePath = [path stringByAppendingPathComponent:fileName];
            folderSize += [self fileSizeAtPath:absolutePath];
        }
        //SDWebImage框架自身计算缓存的实现
        folderSize += [[SDImageCache sharedImageCache] getSize] /1024.0/1024.0;
        return folderSize;
    }
    return 0;
}

// 计算单个文件
- (float)fileSizeAtPath:(NSString *)path{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if([fileManager fileExistsAtPath:path]){
        long long size = [fileManager attributesOfItemAtPath:path error:nil].fileSize;
        return size/1024.0/1024.0;
    }
    return 0;
}

// 清理缓存文件
- (void)clearCache:(NSString *)path{
    NSFileManager *fileManager=[NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:path]) {
        NSArray *childerFiles=[fileManager subpathsAtPath:path];
        for (NSString *fileName in childerFiles) {
//           // 如有需要，加入条件，过滤掉不想删除的文件
//                        if ([fileName isEqualToString:@"ObjectID.plist"]) {
//                            break;
//                        }
            NSString *absolutePath=[path stringByAppendingPathComponent:fileName];
            [fileManager removeItemAtPath:absolutePath error:nil];
        }
    }
    [[SDImageCache sharedImageCache] cleanDisk];
}
#pragma mark - 日记编辑界面(FunctionViewB) 添加视图模板
// 初始视图为 editingViewA
- (void)setUpEditingViews
{
    self.chickedTag = 186010;
    self.editingViewA = [[WYTEditingViewA alloc] initWithFrame:CGRectMake(0, 0, kEditingBoardViewWidth, kEditingBoardViewHeight)];
    [self.rootView.diaryEditingBoardView addSubview:self.editingViewA];
    self.editingViewB = [[LYLEditingViewB alloc] initWithFrame:CGRectMake(0, 0, kEditingBoardViewWidth, kEditingBoardViewHeight)];
    [self.editingViewB.touchImageButton addTarget:self action:@selector(toucheImageButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    self.editingViewC = [[GHEditingViewC alloc] initWithFrame:CGRectMake(0, 0, kEditingBoardViewWidth, kEditingBoardViewHeight)];
    [self.editingViewC.touchImageButton addTarget:self action:@selector(toucheImageButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    self.editingViewD = [[GHEditingViewD alloc] initWithFrame:CGRectMake(0, 0, kEditingBoardViewWidth, kEditingBoardViewHeight)];
    [self.editingViewD.touchImageButton addTarget:self action:@selector(toucheImageButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    self.editingViewE = [[LYLEditingViewE alloc] initWithFrame:CGRectMake(0, 0, kEditingBoardViewWidth, kEditingBoardViewHeight)];
    [self.editingViewE.touchImageButton addTarget:self action:@selector(toucheImageButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    self.editingViewF = [[LYLEditingViewF alloc] initWithFrame:CGRectMake(0, 0, kEditingBoardViewWidth, kEditingBoardViewHeight)];
    [self.editingViewF.touchImageButton addTarget:self action:@selector(toucheImageButtonAction:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)setUpDelegate{
    //设置scrollview代理
    self.rootView.functionScrollView.delegate = self;
    
    //登录界面TextField代理
    self.rootView.loginView.loginUserTextField.delegate = self;
    self.rootView.loginView.loginPasswordTextField.delegate = self;
    self.rootView.registerView.registerUserTextField.delegate = self;
    self.rootView.registerView.registerPasswordTextField.delegate = self;
    self.rootView.registerView.registerAgainTextField.delegate = self;
}

- (void)setUpNotificationCenter{
    [self notificationCenter];
}

- (void)notificationCenter{
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(SwipeDownToDesk:) name:@"SWIPEDOWNTODESK" object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(SwipeUpToDesk:) name:@"SWIPEUPTODESK" object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(swipToHideKeyboard:) name:@"SWIPETOHIDEKEYBOARD" object:nil];
    //跳到选择天气那个界面的通知
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(tapToJumpIntoDiaryInfoView:) name:@"TAPTOJUMPINTODIARYINFOVIEW" object:nil];
    // 跳转子视图控制器通知
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(presentChildController:) name:@"PRESENTTOCHILDCONTROLLER" object:nil];
    // 意见反馈界面上移
//    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(contactUSViewJumpUp) name:@"CONTACTUSVIEWJUMPUP" object:nil];
}
- (void)addShareView
{
    self.shareView = [[FZLShareView alloc] initWithFrame:self.view.bounds];
    self.shareView.alpha = 0;
    [self.shareView.shareSinaButton addTarget:self action:@selector(actionShareSinaButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.shareView.saveToPhotoButton addTarget:self action:@selector(actionSaveToPhotoButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.shareView];
 
    
}
#pragma mark 上拉分享
- (void)SwipeUpToDesk:(NSNotificationCenter *)SwipeUpToDesk{
    self.shareImage = [UIImage captureWithShareView:self.view];
    [UIView animateWithDuration:0.3 animations:^{
        [self.view bringSubviewToFront:self.shareView];
        self.shareView.alpha = 1;
    }];
}

- (void)actionShareSinaButton:(UIButton *)button
{
    [self sharesToWay:UMShareToSina];
    self.shareView.alpha = 0;
}
- (void)sharesToWay: (NSString *)way
{
    // // 友盟分享到人人
    [UMSocialAccountManager isOauthAndTokenNotExpired:way];
    //进入授权页面
    [UMSocialSnsPlatformManager getSocialPlatformWithName:way].loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
        if (response.responseCode == UMSResponseCodeSuccess) {
            //获取微博用户名、uid、token等
            [[UMSocialDataService defaultDataService]  postSNSWithTypes:@[way] content:@"" image:self.shareImage location:nil urlResource:nil presentedController:self completion:^(UMSocialResponseEntity *shareResponse){
                if (shareResponse.responseCode == UMSResponseCodeSuccess) {
                    WYTAlertView *alerView = [WYTAlertView setUpAlertViewWithSucceed:NO alertLabelText:@"发布失败"];
                    [self.view addSubview:alerView];
                } else {
                    WYTAlertView *alerView = [WYTAlertView setUpAlertViewWithSucceed:YES alertLabelText:@"发布成功"];
                    [self.view addSubview:alerView];
                }
            }];
        }
    });
}
//新浪微博
extern NSString *const UMShareToSina;

- (void)actionSaveToPhotoButton:(UIButton *)button
{
    UIImageWriteToSavedPhotosAlbum(self.shareImage, self, nil, nil);
    WYTAlertView *alertView = [WYTAlertView setUpAlertViewWithSucceed:YES alertLabelText:@"保存成功"];
    [self.view addSubview:alertView];
}

- (void)writeToPhotoAlbumSucceed{
    //NSLog(@"save suc");
}

- (void)presentChildController:(NSNotification *)presentVC{
    LYLTimeLineVC *timeLineVC = [[LYLTimeLineVC alloc] init];
    GHAllDiaryVC *allDiarlyVC = [[GHAllDiaryVC alloc] init];
    if([presentVC.object isEqualToString:@"turnLeft"]) {
        [self presentViewController:timeLineVC animated:YES completion:nil];
    } else {
        [self presentViewController:allDiarlyVC animated:YES completion:nil];
    }
}

- (void)SwipeDownToDesk:(NSNotificationCenter *)SwipeDownToDesk{
    //放一个ImageView,里面放截图,用于动画 缩小,它的大小就是纸张的大小
    self.diaryCaptureImageView.frame = CGRectMake(kScreenWidth * 0.02388708, kScreenHeight * 0.03363914, kScreenWidth * 0.9522584, kScreenHeight * 0.84036697);
    
    //里面放的截图是根据editingboardView的区域截出来的图片.
    self.diaryCaptureImageView.image = [UIImage captureWithView:self.rootView.diaryEditingBoardView];

     self.shareImage = self.diaryCaptureImageView.image;
    
    //注掉了,赋值在SwipeUpToDesk里:
    //self.shareImage = self.diaryCaptureImageView.image;
    //加到rootView上,准备动画
    [self.rootView addSubview:self.diaryCaptureImageView];
    
#warning 子线程
    NSBlockOperation *block = [NSBlockOperation blockOperationWithBlock:^{
        // 数据的处理
        [self saveDateWithButtonTag:self.chickedTag];
    }];
    [self.queue addOperation:block];
    self.queue.maxConcurrentOperationCount = 1;
    
    //先将EditingView移出屏幕
    self.rootView.diaryEditingBoardView.frame = CGRectMake(- 736, (kScreenHeight - kEditingBoardViewHeight) / 2, kEditingBoardViewWidth, kEditingBoardViewHeight);

    //开始动画
    [UIView animateWithDuration:0.3f animations:^{
    // 动画的frame变化我改变了一下,用的比例死值,不获取imageViewB的参数了.
        self.rootView.diaryStartEditingImageView.frame = CGRectMake(0, kScreenHeight * 0.20788043, CGRectGetWidth(self.rootView.functionImageViewB.frame), CGRectGetHeight(self.rootView.functionImageViewB.frame));
      
        //动画改变frame让它缩小
        self.diaryCaptureImageView.frame = CGRectMake(CGRectGetWidth(self.rootView.functionImageViewB.frame) * 0.1606949, kScreenHeight * 0.23302, CGRectGetWidth(self.rootView.functionImageViewB.frame) * 0.67969598, CGRectGetHeight(self.rootView.functionImageViewB.frame) * 0.92097836);
        
        self.rootView.diaryStartEditingBlurView.alpha = 0;
        
        //下面一条模板选择scrollView移出
        self.rootView.pageModelScrollView.frame = CGRectMake(0, 800, kScreenWidth, kPageModelScrollViewHeight);
    }];
    self.rootView.functionScrollView.userInteractionEnabled = YES;
    [self performSelector:@selector(delayAfterDiaryEndEditing) withObject:self afterDelay:0.3];
}

- (void)swipToHideKeyboard:(NSNotificationCenter *)swipToHideKeyboard{
    if (self.keyboradWasShown == YES) {
        self.keyboradWasShown = NO;
        [UIView animateWithDuration:0.3 animations:^{
            self.rootView.diaryEditingBoardView.center = self.diaryEditingBoardViewOriginalCenter;
            self.rootView.diaryStartEditingImageView.center = self.diaryStartEditingImageViewOriginalCenter;
        }];
    }else if (self.keyboradWasShown == NO) {
        [[NSNotificationCenter defaultCenter]postNotificationName:@"SWIPEDOWNTODESK" object:nil userInfo:nil];
        
    }
}
#pragma mark - 模板上数据的处理
- (void)saveDateWithButtonTag:(NSInteger)tag
{
    switch (tag) {
        case 186010:
            self.dataModel.clickTag = [NSString stringWithFormat:@"%ld",tag];
            self.dataModel.photoImg = NULL;
            self.dataModel.title = NULL;
            self.dataModel.subtitle = NULL;
            self.dataModel.summary = self.editingViewA.textView.text;
            self.dataModel.dateLabelText = self.editingViewA.dateLabel.text;
            self.dataModel.placeLabel = NULL;
            self.dataModel.diaryImage = self.diaryCaptureImageView.image;
            [self.handle saveDataWithManageContext:self.manageContext AndWithModel:self.dataModel];
            break;
        case 186011:
            self.dataModel.clickTag = [NSString stringWithFormat:@"%ld",tag];
            self.dataModel.photoImg = self.editingViewB.topImgView.image;
            self.dataModel.title = self.editingViewB.titleField.text;
            self.dataModel.subtitle = self.editingViewB.subheadingsField.text;
            self.dataModel.summary = NULL;
            self.dataModel.dateLabelText = self.editingViewB.dateLabel.text;
            self.dataModel.placeLabel = self.editingViewB.placeLabel.text;
            self.dataModel.diaryImage = self.diaryCaptureImageView.image;
            [self.handle saveDataWithManageContext:self.manageContext AndWithModel:self.dataModel];
            break;
        case 186012:
            self.dataModel.clickTag = [NSString stringWithFormat:@"%ld",tag];
            self.dataModel.photoImg = self.editingViewC.pic_img.image;
            self.dataModel.title = self.editingViewC.titleTextV.text;
            self.dataModel.subtitle = NULL;
            self.dataModel.summary = self.editingViewC.summaryTextV.text;
            self.dataModel.dateLabelText = self.editingViewC.authorLabel.text;
            self.dataModel.placeLabel = self.editingViewC.addressLabel.text;
            self.dataModel.diaryImage = self.diaryCaptureImageView.image;
            [self.handle saveDataWithManageContext:self.manageContext AndWithModel:self.dataModel];
            
            break;
        case 186013:
            self.dataModel.clickTag = [NSString stringWithFormat:@"%ld",tag];
            self.dataModel.photoImg = self.editingViewD.pic_img.image;
            self.dataModel.title = self.editingViewD.titleTextF.text;
            self.dataModel.subtitle = NULL;
            self.dataModel.summary = NULL;
            self.dataModel.dateLabelText = self.editingViewD.authorAndDateLabel.text;
            self.dataModel.placeLabel = self.editingViewD.addressLabel.text;
            self.dataModel.diaryImage = self.diaryCaptureImageView.image;
            [self.handle saveDataWithManageContext:self.manageContext AndWithModel:self.dataModel];
            break;
        case 186014:
            self.dataModel.clickTag = [NSString stringWithFormat:@"%ld",tag];
            self.dataModel.photoImg = self.editingViewE.topImgView.image;
            self.dataModel.title = self.editingViewE.titleField.text;
            self.dataModel.subtitle = NULL;
            self.dataModel.summary = self.editingViewE.bodyView.text;
            self.dataModel.dateLabelText = self.editingViewE.dateLabel.text;
            self.dataModel.placeLabel = self.editingViewE.placeLabel.text;
            self.dataModel.diaryImage = self.diaryCaptureImageView.image;
            [self.handle saveDataWithManageContext:self.manageContext AndWithModel:self.dataModel];
            break;
        case 186015:
            self.dataModel.clickTag = [NSString stringWithFormat:@"%ld",tag];
            self.dataModel.photoImg = self.editingViewF.topImgView.image;
            self.dataModel.title = self.editingViewF.nameField.text;
            self.dataModel.subtitle = NULL;
            self.dataModel.summary = self.editingViewF.bodyView.text;
            self.dataModel.dateLabelText = self.editingViewF.dateLabel.text;
            self.dataModel.placeLabel = self.editingViewF.placeLabel.text;
            self.dataModel.diaryImage = self.diaryCaptureImageView.image;
            [self.handle saveDataWithManageContext:self.manageContext AndWithModel:self.dataModel];
            break;
        default:
            break;
    }
}

- (void)setUpButtonActions{
    [self.rootView.diaryButton addTarget:self action:@selector(functionButtonDidClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.rootView.checkInButton addTarget:self action:@selector(functionButtonDidClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.rootView.shorthandButton addTarget:self action:@selector(functionButtonDidClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.rootView.diaryStartEditingButton addTarget:self action:@selector(diaryStartEditing) forControlEvents:UIControlEventTouchUpInside];
    
    self.rootView.pageModelABtn.tag = 186010;
    [self.rootView.pageModelABtn addTarget:self action:@selector(diaryPageModelDidSelected:) forControlEvents:UIControlEventTouchUpInside];
    
    self.rootView.pageModelBBtn.tag = 186011;
    [self.rootView.pageModelBBtn addTarget:self action:@selector(diaryPageModelDidSelected:) forControlEvents:UIControlEventTouchUpInside];
    
    self.rootView.pageModelCBtn.tag = 186012;
    [self.rootView.pageModelCBtn addTarget:self action:@selector(diaryPageModelDidSelected:) forControlEvents:UIControlEventTouchUpInside];
    
    self.rootView.pageModelDBtn.tag = 186013;
    [self.rootView.pageModelDBtn addTarget:self action:@selector(diaryPageModelDidSelected:) forControlEvents:UIControlEventTouchUpInside];
    
    self.rootView.pageModelEBtn.tag = 186014;
    [self.rootView.pageModelEBtn addTarget:self action:@selector(diaryPageModelDidSelected:) forControlEvents:UIControlEventTouchUpInside];
    
    self.rootView.pageModelFBtn.tag = 186015;
    [self.rootView.pageModelFBtn addTarget:self action:@selector(diaryPageModelDidSelected:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)setUpGestures{
    //    //下滑手势,加在了正在编辑的self.TextView上. textView在之后会被重新赋值自定义的模板View
    //    UISwipeGestureRecognizer *swipeDownGR = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeDown:)];
    //    swipeDownGR.direction = UISwipeGestureRecognizerDirectionDown;
    //    [self.rootView.diaryStartEditingImageView addGestureRecognizer:swipeDownGR];
}

#pragma mark 控件代理
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
// 现在有两个ScrollView:大ScrollView和日记编辑界面选择模板ScrollView共用DidEndDecelerating,暂未出现冲突
    //大ScrollView停止滑动后,将三个FunctionButton,三个伪pageView圆的Alpha值调整.
    if (self.rootView.functionScrollView.contentOffset.x == kScreenWidth * 2) {
        [UIView animateWithDuration:0.5f animations:^{
            self.rootView.shorthandDirectionButton.alpha = 1;
            self.rootView.shorthandDirectionButtonLineView.alpha = 1;
            
            [self.rootView.diaryButton setBackgroundImage:[UIImage imageNamed:@"diary_item"] forState:0];
            self.rootView.shorthandButton.alpha = 0.9;
            self.rootView.checkInButton.alpha = 0.3;
            self.rootView.diaryButton.alpha = 0.3;
            
            self.rootView.circleViewRight.alpha = 0.9;
            self.rootView.circleViewLeft.alpha = 0.3;
            self.rootView.circleViewMid.alpha = 0.3;
        }];
    }else if(self.rootView.functionScrollView.contentOffset.x == kScreenWidth * 1){
        [UIView animateWithDuration:0.5f animations:^{
            self.rootView.shorthandDirectionButton.alpha = 0;
            self.rootView.shorthandDirectionButtonLineView.alpha = 0;
            
            [self.rootView.diaryButton setBackgroundImage:[UIImage imageNamed:@"diaryAdd"] forState:0];
            self.rootView.diaryButton.alpha = 0.9;
            self.rootView.checkInButton.alpha = 0.3;
            self.rootView.shorthandButton.alpha = 0.3;
            
            self.rootView.circleViewMid.alpha = 0.9;
            self.rootView.circleViewLeft.alpha = 0.3;
            self.rootView.circleViewRight.alpha = 0.3;
        }];
    }else{
        [UIView animateWithDuration:0.5f animations:^{
            self.rootView.shorthandDirectionButton.alpha = 0;
            self.rootView.shorthandDirectionButtonLineView.alpha = 0;
            
            [self.rootView.diaryButton setBackgroundImage:[UIImage imageNamed:@"diary_item"] forState:0];
            self.rootView.checkInButton.alpha = 0.9;
            self.rootView.diaryButton.alpha = 0.3;
            self.rootView.shorthandButton.alpha = 0.3;
            
            self.rootView.circleViewLeft.alpha = 0.9;
            self.rootView.circleViewRight.alpha = 0.3;
            self.rootView.circleViewMid.alpha = 0.3;
        }];
    }
    
    BOOL a = [self.rootView.loginView.loginUserTextField isFirstResponder];
    BOOL b = [self.rootView.loginView.loginPasswordTextField isFirstResponder];
    BOOL c = [self.rootView.registerView.registerUserTextField isFirstResponder];
    BOOL d = [self.rootView.registerView.registerAgainTextField isFirstResponder];
    BOOL e = [self.rootView.registerView.registerPasswordTextField isFirstResponder];
    
    if (a) {
        [self.rootView.loginView.loginUserTextField resignFirstResponder];
    }else if (b){
        [self.rootView.loginView.loginPasswordTextField resignFirstResponder];
    }else if (c){
        [self.rootView.registerView.registerUserTextField resignFirstResponder];
    }else if (d){
        [self.rootView.registerView.registerAgainTextField resignFirstResponder];
    }else if (e){
        [self.rootView.registerView.registerPasswordTextField resignFirstResponder];
    }
    
    if (scrollView.contentOffset.x == kScreenWidth * 2) {
        self.rootView.settingsDirectionView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
        [UIView animateWithDuration:0.3 animations:^{
            self.rootView.settingsDirectionView.alpha = 0.8;
        }];
    }
}

#pragma mark Button点击事件
- (void)functionButtonDidClicked:(UIButton *)sender{
    if (sender.tag == 186000) {
        [self.rootView.functionScrollView setContentOffset:CGPointMake(kScreenWidth * 0, 0) animated:YES];
        [UIView animateWithDuration:0.5f animations:^{
            self.rootView.shorthandDirectionButton.alpha = 0;
            self.rootView.shorthandDirectionButtonLineView.alpha = 0;
            
            [self.rootView.diaryButton setBackgroundImage:[UIImage imageNamed:@"diary_item"] forState:0];
            self.rootView.checkInButton.alpha = 0.9;
            self.rootView.diaryButton.alpha = 0.3;
            self.rootView.shorthandButton.alpha = 0.3;
            
            self.rootView.circleViewLeft.alpha = 0.9;
            self.rootView.circleViewRight.alpha = 0.3;
            self.rootView.circleViewMid.alpha = 0.3;
        }];
    }else if(sender.tag == 186001){
        if (self.rootView.functionScrollView.contentOffset.x == kScreenWidth) {
            [self deleteAllDataWithDiaryModel];
        } else {
            [self.rootView.functionScrollView setContentOffset:CGPointMake(kScreenWidth * 1, 0) animated:YES];
            [UIView animateWithDuration:0.5f animations:^{
                self.rootView.shorthandDirectionButton.alpha = 0;
                self.rootView.shorthandDirectionButtonLineView.alpha = 0;
                
                [self.rootView.diaryButton setBackgroundImage:[UIImage imageNamed:@"diaryAdd"] forState:0];
                self.rootView.diaryButton.alpha = 0.9;
                self.rootView.checkInButton.alpha = 0.3;
                self.rootView.shorthandButton.alpha = 0.3;
                
                self.rootView.circleViewMid.alpha = 0.9;
                self.rootView.circleViewLeft.alpha = 0.3;
                self.rootView.circleViewRight.alpha = 0.3;
            }];
        }
    }else if (sender.tag == 186002){
        [self.rootView.functionScrollView setContentOffset:CGPointMake(kScreenWidth * 2, 0) animated:YES];
        [UIView animateWithDuration:0.5f animations:^{
            self.rootView.shorthandDirectionButton.alpha = 1;
            self.rootView.shorthandDirectionButtonLineView.alpha = 1;
            
            [self.rootView.diaryButton setBackgroundImage:[UIImage imageNamed:@"diary_item"] forState:0];
            self.rootView.shorthandButton.alpha = 0.9;
            self.rootView.checkInButton.alpha = 0.3;
            self.rootView.diaryButton.alpha = 0.3;
            
            self.rootView.circleViewRight.alpha = 0.9;
            self.rootView.circleViewLeft.alpha = 0.3;
            self.rootView.circleViewMid.alpha = 0.3;
        }];
    }
}

#pragma mark - 点击清空模板上的数据
- (void)deleteAllDataWithDiaryModel
{
    self.editingViewA.textView.text = nil;
    self.editingViewB.topImgView.layer.contents = (id)[UIImage imageNamed:@"imgPlaceHolderEVB"].CGImage;
    self.editingViewB.titleField.text = nil;
    self.editingViewC.pic_img.layer.contents = (id)[UIImage imageNamed:@"imgPlaceHolderEVC"].CGImage;
    self.editingViewC.titleTextV.text = nil;
    self.editingViewC.summaryTextV.text = nil;
    self.editingViewD.pic_img.layer.contents = (id)[UIImage imageNamed:@"imgPlaceHolderEVD"].CGImage;
    self.editingViewD.titleTextF.text = nil;
    self.editingViewE.topImgView.layer.contents = (id)[UIImage imageNamed:@"imgPlaceHolderEVE"].CGImage;
    self.editingViewE.titleField.text = nil;
    self.editingViewE.bodyView.text = nil;
    self.editingViewF.topImgView.layer.contents = (id)[UIImage imageNamed:@"imgPlaceHolderEVF"].CGImage;
    self.editingViewF.nameField.text = nil;
    self.editingViewF.bodyView.text = nil;
    [self diaryStartEditing];
}

#pragma mark 将要开始编辑
- (void)diaryStartEditing {
    //关掉主scrollView的交互
    self.rootView.functionScrollView.userInteractionEnabled = NO;
    
    //把1:铺着日记纸的imageView, 2:日记纸下的blurView, 3:进入日记编辑界面后的下拉教程imageView拉回来
    self.rootView.diaryStartEditingBlurView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    self.rootView.diaryStartEditingImageView.frame = CGRectMake(CGRectGetMinX(self.rootView.functionImageViewB.frame) - kScreenWidth, CGRectGetMinY(self.rootView.functionScrollView.frame) + CGRectGetMinY(self.rootView.functionImageViewB.frame), CGRectGetWidth(self.rootView.functionImageViewB.frame), CGRectGetHeight(self.rootView.functionImageViewB.frame));
    self.rootView.directionView.frame = CGRectMake(0, 0, kScreenWidth, kScreenWidth / 9 * 16);
    [self performSelector:@selector(showDirectionView) withObject:self afterDelay:1.3];
    [self.rootView bringSubviewToFront:self.rootView.diaryStartEditingImageView];
    
    //将blurView先设成透明,等待在下面的动画里过度成可显示
    self.rootView.diaryStartEditingBlurView.alpha = 0;
    
    //计算日记纸的扩大到全屏后的新宽高,准备开始动画
    CGFloat increaseScale = 1.4; //纸张的扩大比例
    CGFloat diaryStartEditingImageViewNewWidth = CGRectGetWidth(self.rootView.diaryStartEditingImageView.frame) * increaseScale;
    CGFloat diaryStartEditingImageViewNewHeight = CGRectGetHeight(self.rootView.diaryStartEditingImageView.frame) * increaseScale;
    
    [self.rootView.diaryStartEditingImageView addSubview:self.screenShotImageViewOnFunctionImageViewB];
    
    //开始动画
    [UIView animateWithDuration:0.3f animations:^{
        
        CGFloat x = self.screenShotImageViewOnFunctionImageViewB.frame.origin.x;
        CGFloat y = self.screenShotImageViewOnFunctionImageViewB.frame.origin.y;
        CGFloat width = self.screenShotImageViewOnFunctionImageViewB.frame.size.width;
        CGFloat height = self.screenShotImageViewOnFunctionImageViewB.frame.size.height;
        
        self.screenShotImageViewOnFunctionImageViewB.frame = CGRectMake(x * 1.4, y * 1.4, width * 1.4, height * 1.4);
        
        //日记纸张imageView变大,已经超出屏幕范围了
        self.rootView.diaryStartEditingImageView.frame = CGRectMake(-(diaryStartEditingImageViewNewWidth - kScreenWidth) / 2, 0, diaryStartEditingImageViewNewWidth, diaryStartEditingImageViewNewHeight);
        //blurView显示
        self.rootView.diaryStartEditingBlurView.alpha = 1;
        //下方选择模板的scrollView浮上来
        self.rootView.pageModelScrollView.frame = CGRectMake(0, kPageModelScrollViewMinY, kScreenWidth, kPageModelScrollViewHeight);
    }];
    
    //动画完成后执行的延迟操作
    //动画执行完成后再铺editingView页面
    [self performSelector:@selector(diaryReadyForEditing) withObject:self afterDelay:0.3f];
}

#pragma mark 日记TextView准备好
- (void)diaryReadyForEditing{
    self.rootView.diaryEditingBoardView.frame = CGRectMake((kScreenWidth - kEditingBoardViewWidth) / 2, kScreenHeight * 0.035, kEditingBoardViewWidth, kEditingBoardViewHeight);
    [self.rootView bringSubviewToFront:self.rootView.diaryEditingBoardView];
    [self.screenShotImageViewOnFunctionImageViewB removeFromSuperview];
    [self.diaryCaptureImageView removeFromSuperview];
}

#pragma mark 选择日记模板点击事件
- (void)diaryPageModelDidSelected:(UIButton *)sender
{
    NSArray *editingBoardSubViews = [self.rootView.diaryEditingBoardView subviews];
    if (editingBoardSubViews.count > 0) {
        UIView *removedView = editingBoardSubViews[0];
        [removedView removeFromSuperview];
    }
    switch (sender.tag) {
        case 186010:
            [self.rootView.diaryEditingBoardView addSubview:self.editingViewA];
            break;
        case 186011:
            [self.rootView.diaryEditingBoardView addSubview:self.editingViewB];
            break;
        case 186012:
            [self.rootView.diaryEditingBoardView addSubview:self.editingViewC];
            break;
        case 186013:
            [self.rootView.diaryEditingBoardView addSubview:self.editingViewD];
            break;
        case 186014:
            [self.rootView.diaryEditingBoardView addSubview:self.editingViewE];
            break;
        case 186015:
            [self.rootView.diaryEditingBoardView addSubview:self.editingViewF];
            break;
        default:
            break;
    }
    self.chickedTag = sender.tag;
}

#pragma mark - 添加图片的点击事件
- (void)toucheImageButtonAction:(UIButton*)sender
{
    UIImagePickerController *pic = [[UIImagePickerController alloc] init];
    pic.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    pic.delegate = self;
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"是否对图片进行裁剪" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    // 对图片进行裁剪的按钮
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"选择照片某一部分" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        pic.allowsEditing = YES;
        [self presentViewController:pic animated:YES completion:nil];
    }];
    // 显示原图的按钮
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"自适应明信片框" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self presentViewController:pic animated:YES completion:nil];
    }];
    // 取消按钮
    UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alert addAction:action1];
    [alert addAction:action2];
    [alert addAction:action3];
    [self presentViewController:alert animated:YES completion:nil];
}


#pragma mark - 添加图片点击事件的代理方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary<NSString *,id> *)editingInfo
{
    switch (self.chickedTag) {
        case 186011:
            self.editingViewB.topImgView.image = image;
            break;
        case 186012:
            self.editingViewC.pic_img.image = image;
            break;
        case 186013:
            self.editingViewD.pic_img.image = image;
            break;
        case 186014:
            self.editingViewE.topImgView.image = image;
            break;
        case 186015:
            self.editingViewF.topImgView.image = image;
            break;
        default:
            break;
    }
    [picker dismissViewControllerAnimated:YES completion:nil];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark 将要结束编辑
- (void)diaryEndEditing:(UIButton *)sender{
    
}

#pragma mark 结束编辑后,将控件移出的延迟事件
- (void)delayAfterDiaryEndEditing{
    //等blurView完全消失后,将它移出屏幕
    self.rootView.diaryStartEditingBlurView.frame = CGRectMake(-414, -736, kScreenWidth, kScreenHeight);
    //日记纸张界面
    self.rootView.diaryStartEditingImageView.frame = CGRectMake(-414, -736, CGRectGetWidth(self.rootView.functionImageViewB.frame), CGRectGetHeight(self.rootView.functionImageViewB.frame));
    
    //建立一个小的截图imageview贴到桌面上被缩小的纸张上去
    self.screenShotImageViewOnFunctionImageViewB.frame = CGRectMake(CGRectGetWidth(self.rootView.functionImageViewB.frame) * 0.1606949, CGRectGetHeight(self.rootView.functionImageViewB.frame) * 0.03762935, CGRectGetWidth(self.rootView.functionImageViewB.frame) * 0.67969598, CGRectGetHeight(self.rootView.functionImageViewB.frame) * 0.92097836);
    if ([WYTDevicesTool iPhone4_iPhone4s]) {
        self.screenShotImageViewOnFunctionImageViewB.frame = CGRectMake(CGRectGetWidth(self.rootView.functionImageViewB.frame) * 0.2006949, CGRectGetHeight(self.rootView.functionImageViewB.frame) * 0.03762935, CGRectGetWidth(self.rootView.functionImageViewB.frame) * 0.58969598, CGRectGetHeight(self.rootView.functionImageViewB.frame) * 0.78097836);
    }
    
    self.screenShotImageViewOnFunctionImageViewB.image = self.diaryCaptureImageView.image;
    [self.diaryCaptureImageView removeFromSuperview];
    [self.rootView.functionImageViewB addSubview:self.screenShotImageViewOnFunctionImageViewB];
    
#warning 将截图ImageView移出屏幕
    self.diaryCaptureImageView.frame = CGRectMake(0, 740, kScreenWidth, kScreenHeight);
}

#pragma mark 下滑事件
- (void)swipeDown:(UISwipeGestureRecognizer *)sender{
    
}

//天气选择界面收到通知实现方法
- (void)tapToJumpIntoDiaryInfoView:(NSNotificationCenter *)sender{
    //NSLog(@"收到通知,准备跳转到DiaryInfoSelectionView");
    
    WYTDiaryInfoSelectionVC *diaryInfoSelectionVC = [[WYTDiaryInfoSelectionVC alloc]init];
    diaryInfoSelectionVC.delegate = self;
    
    self.rootView.homeKeyView.alpha = 0;
    UIImage *backgroundCaptureImageForFakeImage = [UIImage captureWithView:self.rootView];
    diaryInfoSelectionVC.backgroundImageForFakeImage = backgroundCaptureImageForFakeImage;
    self.rootView.homeKeyView.alpha = 1;
    [self presentViewController:diaryInfoSelectionVC animated:NO completion:nil];
    
    [self performSelector:@selector(JumpOutDiaryInfoViewDelay) withObject:self afterDelay:0.5];
}

- (void)tapToJumpOutDiaryInfoView:(NSNotificationCenter *)sender{
    
}

- (void)JumpOutDiaryInfoViewDelay{
    [self.diaryInfoSelectionBlurView removeFromSuperview];
    self.diaryInfoSelectionBlurView = nil;
    [self.infoSelectionView removeFromSuperview];
    self.infoSelectionView = nil;
}

#pragma - 教程界面
//显示
- (void)showDirectionView{
    [self.rootView bringSubviewToFront:self.rootView.directionView];
    self.rootView.directionView.userInteractionEnabled = YES;
    [UIView animateWithDuration:0.3 animations:^{
        self.rootView.directionView.alpha = 0.8;
    }];
    
    UITapGestureRecognizer *directionViewTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(directionViewTap:)];
    [self.rootView.directionView addGestureRecognizer:directionViewTap];
}
//添加手势
- (void)directionViewTap:(UITapGestureRecognizer *)sender{
    [self.rootView.directionView removeFromSuperview];
    self.rootView.directionView = nil;
}

#pragma mark - 九宫格


#pragma mark 计算当前弹出键盘的高度
- (void)registerForKeyboardNotifications
{
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWasHidden:) name:UIKeyboardDidHideNotification object:nil];
}

- (void)keyboardWasShown:(NSNotification *)notif
{
    self.keyboradWasShown = YES;
    NSDictionary *info = [notif userInfo];
    NSValue *value = [info objectForKey:UIKeyboardFrameBeginUserInfoKey];
    CGSize keyboradSize = [value CGRectValue].size;
    CGFloat keyboradHeight = keyboradSize.height;
    self.keyboradHight = keyboradHeight;
    // ---------- PageChose ---------
    if (self.locationChanged == NO) {
        if (self.chickedTag == 186010) {
            CGFloat newHeight = kEditingBoardViewHeight*0.82 - keyboradHeight + kPageSubViewsRemoveY;
            CGRect newframe = self.editingViewA.textView.frame;
            newframe.size.height = newHeight;
            self.editingViewA.textView.frame = newframe;
        }
        else if (self.chickedTag > 186010) {
            // 记录初识中心点 ,用于返回
            self.diaryEditingBoardViewOriginalCenter = self.rootView.diaryEditingBoardView.center;
            self.diaryStartEditingImageViewOriginalCenter = self.rootView.diaryStartEditingImageView.center;
            // 变化
            [UIView animateWithDuration:0.3 animations:^{
                self.rootView.diaryEditingBoardView.center = CGPointMake(self.rootView.diaryEditingBoardView.center.x, self.rootView.diaryEditingBoardView.center.y - keyboradHeight + kPageSubViewsRemoveY);
                self.rootView.diaryStartEditingImageView.center = CGPointMake(self.rootView.diaryStartEditingImageView.center.x, self.rootView.diaryStartEditingImageView.center.y - keyboradHeight + kPageSubViewsRemoveY);
            }];
        }
        self.locationChanged = YES;
    }
}

- (void)keyboardWasHidden:(NSNotification *)notif
{
    //NSLog(@"下滑回收键盘");
    self.locationChanged = NO;
    if (self.chickedTag > 186010) {
        self.keyboradWasShown = NO;
        [UIView animateWithDuration:0.3 animations:^{
            self.rootView.diaryEditingBoardView.center = self.diaryEditingBoardViewOriginalCenter;
            self.rootView.diaryStartEditingImageView.center = self.diaryStartEditingImageViewOriginalCenter;
        }];
    } else if (self.chickedTag == 186010){
        [UIView animateWithDuration:0.3 animations:^{
            CGFloat newHeight = kEditingBoardViewHeight*0.82;
            CGRect newframe = self.editingViewA.textView.frame;
            newframe.size.height = newHeight;
            self.editingViewA.textView.frame = newframe;
        }];
    }
}

//- (void)popDownSwipChick:(UISwipeGestureRecognizer *)swipe
//{
//}

#pragma mark - 打卡界面

- (void)setUpLoginRegisterActions{
    [self.rootView.loginView.loginRegisterButton addTarget:self action:@selector(loginRegisterButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.rootView.loginView.loginFinishButton addTarget:self action:@selector(loginFinishButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.rootView.loginView.loginCancelButton addTarget:self action:@selector(loginCancelButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.rootView.registerView.registerFinishButton addTarget:self action:@selector(registerFinishButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.rootView.registerView.registerCancelButton addTarget:self action:@selector(registerCancelButtonAction:) forControlEvents:UIControlEventTouchUpInside];
}
#pragma ---注册----
- (void)loginRegisterButtonAction:(UIButton *)sender{
    
    self.rootView.registerView.alpha = 0;
    self.rootView.registerView.frame = CGRectMake(0, 0, CGRectGetWidth(self.rootView.functionImageViewA.frame), CGRectGetHeight(self.rootView.functionImageViewA.frame));
    
    [UIView animateWithDuration:0.3 animations:^{
        self.rootView.loginView.alpha = 0;
        self.rootView.registerView.alpha = 1;
    }];
}
#pragma ---登陆----
-(void)loginFinishButtonAction:(UIButton *)sender{
    if ([self.rootView.loginView.loginUserTextField.text isEqualToString:@""] || [self.rootView.loginView.loginPasswordTextField.text isEqualToString:@""]) {
        WYTAlertView *alertView = [WYTAlertView setUpAlertViewWithSucceed:NO alertLabelText:@"用户名密码不能为空"];
        [self.rootView addSubview:alertView];
    } else {
    [AVUser logInWithUsernameInBackground:self.rootView.loginView.loginUserTextField.text password:self.rootView.loginView.loginPasswordTextField.text block:^(AVUser *user, NSError *error) {
        if (error) {
            NSInteger codeNum = [[error.userInfo valueForKey:@"code"] integerValue];
            if (codeNum == 210){
                WYTAlertView *alertView = [WYTAlertView setUpAlertViewWithSucceed:NO alertLabelText:@"用户名或者密码错误"];
                [self.rootView addSubview:alertView];
            } else if (codeNum == 211){
                WYTAlertView *alertView = [WYTAlertView setUpAlertViewWithSucceed:NO alertLabelText:@"没有这个用户"];
                [self.rootView addSubview:alertView];
            }
        } else {
            WYTAlertView *alertView = [WYTAlertView setUpAlertViewWithSucceed:YES alertLabelText:@"登录成功"];
            [self.rootView addSubview:alertView];
            
        }
    }];
    }
}

- (void)loginCancelButtonAction:(UIButton *)sender{
    
    self.rootView.loginView.loginUserTextField.text = @"";
    self.rootView.loginView.loginPasswordTextField.text = @"";
    
    self.rootView.loginView.loginUserTextField.placeholder = @"登录来同步";
}

- (void)registerFinishButtonAction:(UIButton *)sender{
    if ([self.rootView.registerView.registerUserTextField.text isEqualToString:@""] || [self.rootView.registerView.registerPasswordTextField.text isEqualToString:@""] || [self.rootView.registerView.registerAgainTextField.text isEqualToString:@""]){
        WYTAlertView *alertView = [WYTAlertView setUpAlertViewWithSucceed:NO alertLabelText:@"用户名,密码不能为空"];
        [self.rootView addSubview:alertView];
        // 如果不为空的话
    } else if(![self.rootView.registerView.registerPasswordTextField.text isEqualToString:self.rootView.registerView.registerAgainTextField.text]){
        // 判断两个密码是不是一样
        WYTAlertView *alertView = [WYTAlertView setUpAlertViewWithSucceed:NO alertLabelText:@"密码输入不一致"];
        [self.rootView addSubview:alertView];
        // 输入的正确
    } else  if(self.rootView.registerView.registerPasswordTextField.text.length < 6){
        WYTAlertView *alertView = [WYTAlertView setUpAlertViewWithSucceed:NO alertLabelText:@"密码至少6个字符"];
        [self.rootView addSubview:alertView];
    } else {
        [self registerWithUserName:self.rootView.registerView.registerUserTextField.text UserPassWord:self.rootView.registerView.registerPasswordTextField.text];
     
    }
}

// 注册
- (void)registerWithUserName:(NSString *)name UserPassWord:(NSString *)passWord
{
    AVUser *user = [AVUser user];
    user.username = name;
    user.password = passWord;
    [user signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (error) {
            NSInteger codeNum = [[error.userInfo valueForKey:@"code"] integerValue];
            switch (codeNum) {
                case 202:{
                    WYTAlertView *alertView = [WYTAlertView setUpAlertViewWithSucceed:NO alertLabelText:@"用户名已经被占用"];
                    [self.rootView addSubview:alertView];
                    break;
                }
                default:
                    break;
            }
        } else {
            WYTAlertView *alertView = [WYTAlertView setUpAlertViewWithSucceed:YES alertLabelText:@"登录成功"];
            [self.rootView addSubview:alertView];
            [UIView animateWithDuration:0.3 animations:^{
                self.rootView.registerView.alpha = 0;
                self.rootView.loginView.alpha = 1;
            }];
            [self performSelector:@selector(removeRegisterViewOut) withObject:self afterDelay:0.4];
        }
    }];
}

- (void)registerCancelButtonAction:(UIButton *)sender{
    [UIView animateWithDuration:0.3 animations:^{
        self.rootView.registerView.alpha = 0;
        self.rootView.loginView.alpha = 1;
    }];
    [self performSelector:@selector(removeRegisterViewOut) withObject:self afterDelay:0.4];
}

- (void)removeRegisterViewOut{
    self.rootView.registerView.registerUserTextField.text = nil;
    self.rootView.registerView.registerPasswordTextField.text = nil;
    self.rootView.registerView.registerAgainTextField.text = nil;
    self.rootView.registerView.frame = CGRectMake(0, -736, CGRectGetWidth(self.rootView.functionImageViewA.frame), CGRectGetHeight(self.rootView.functionImageViewA.frame));
}

#pragma mark登录界面代理方法优化
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    CGRect originalFrame = self.rootView.functionImageViewA.frame;
    [UIView animateWithDuration:0.3 animations:^{
        self.rootView.functionImageViewA.frame = CGRectMake(originalFrame.origin.x, originalFrame.origin.y - kScreenHeight * 0.1, originalFrame.size.width, originalFrame.size.height);
    }];
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    CGRect originalFrame = self.rootView.functionImageViewA.frame;
    [UIView animateWithDuration:0.3 animations:^{
        self.rootView.functionImageViewA.frame = CGRectMake(originalFrame.origin.x, originalFrame.origin.y + kScreenHeight * 0.1, originalFrame.size.width, originalFrame.size.height);
    }];
}

#pragma mark - button点击事件用于跳转界面
- (void)changeEditState:(id)sender
{
    
    
}

// 懒加载
- (NSManagedObjectContext *)manageContext
{
    if (!_manageContext) {
        _manageContext = ((AppDelegate *)[UIApplication sharedApplication].delegate).managedObjectContext;
    }
    return _manageContext;
}

#pragma GHDiaryDataModel的懒加载
- (GHDiaryDataModel *)dataModel
{
    if (!_dataModel) {
        _dataModel = [[GHDiaryDataModel alloc] init];
    }
    return _dataModel;
}

#pragma mark 视图的懒加载
- (UIImageView *)diaryCaptureImageView
{
    if (!_diaryCaptureImageView) {
        _diaryCaptureImageView = [[UIImageView alloc] init];
    }
    return _diaryCaptureImageView;
}

- (UIImageView *)screenShotImageViewOnFunctionImageViewB
{
    if (!_screenShotImageViewOnFunctionImageViewB) {
        _screenShotImageViewOnFunctionImageViewB = [[UIImageView alloc] init];
    }
    return _screenShotImageViewOnFunctionImageViewB;
}

#pragma mark - 队列懒加载
- (NSOperationQueue *)queue
{
    if (!_queue) {
        _queue = [[NSOperationQueue alloc] init];
    }
    return _queue;
}

#pragma mark - 同步到云端界面懒加载
- (UILabel *)tipsView
{
    if (!_tipsView) {
        _tipsView = [[UILabel alloc] init];
    }
    return _tipsView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
